package asionreachel.cn.smarthomedemo.widget.filter;

import android.content.Context;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.ImageView;

import asionreachel.cn.smarthomedemo.R;

/**
 * Created by Simon(lyd) on 2016/9/27:06.
 * 描述：带有点击效果的imageview
 */

public class FilterImageView2 extends ImageView {

    public FilterImageView2(Context context) {
        this(context, null);
    }

    public FilterImageView2(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    private Handler handler;

    public FilterImageView2(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
//        setSelected(true);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        switch (event.getActionMasked()) {
            case MotionEvent.ACTION_DOWN:
                /** 设置滤镜 */
                setFilter();
                break;
            case MotionEvent.ACTION_UP:
                /** 传递点击 */
                performClick();
            case MotionEvent.ACTION_CANCEL:
                /** 删除滤镜 */
                removeFilter();
                break;
            default:
                break;
        }
        return true;
    }


    /**
     * 设置滤镜
     */
    private void setFilter() {
        //先获取设置的src图片
        Drawable drawable = getDrawable();
        //当src图片为Nul l，获取背景图片
        if (drawable == null) {
            drawable = getBackground();
        }
        if (drawable != null) {
            //设置滤镜
            drawable.setColorFilter(Color.GRAY, PorterDuff.Mode.MULTIPLY);
        }
    }

    /**
     * 清除滤镜
     */
    private void removeFilter() {
        //先获取设置的src图片
        Drawable drawable = getDrawable();
        //当src图片为Null，获取背景图片
        if (drawable == null) {
            drawable = getBackground();
        }
        if (drawable != null) {
            //清除滤镜
            drawable.clearColorFilter();
        }
    }

    private int state;

    /**
     * @return 0:默认 1:自动 2:自定义
     */
    public void setState(int state) {
        this.state = state;
        if (state == 0) {
            this.setImageResource(R.mipmap.img_plan);
        } else if (state == 1) {
            this.setImageResource(R.mipmap.icon_mode_6_false);
        } else if (state == 2) {
            this.setImageResource(R.mipmap.icon_mode_6_true);
        }
    }

    public int getState() {
        return state;
    }
}
